import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Divider, Table} from 'antd';
import {TABLE, DATE_FORMAT, DATETIME_FORMAT} from '../constants';
import {conventEnumValueToString, formatDateTime, formatAmount} from '../utils';
import {idType, gender, customerOccupationType, industryType, education, householdIncome, customerType, hobby, favoriteVehicleFactor, ownedVehicleBrand, currency} from '../Enum';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
const {Description} = DescriptionList;

export class DetailPanel extends React.PureComponent {
    state = {
        loading: false,
    };
    componentDidMount() {
        this.props.init();
    }
    render() {
        const detailData = this.props.detail || {};
        const {details} = detailData;
        const {formatMessage} = this.props.intl;
        const columns = [{
            title: formatMessage({
                id: 'detailPanel.column.brandName',
                defaultMessage: '品牌'
            }),
            dataIndex: 'brandName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.productCategoryCode',
                defaultMessage: '车型编号'
            }),
            dataIndex: 'productCategoryCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.productCode',
                defaultMessage: '产品编号'
            }),
            dataIndex: 'productCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.productName',
                defaultMessage: '产品名称'
            }),
            dataIndex: 'productName',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.vin',
                defaultMessage: 'VIN'
            }),
            dataIndex: 'vin',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.engineCode',
                defaultMessage: '发动机号'
            }),
            dataIndex: 'engineCode',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.licensePlate',
                defaultMessage: '车牌号'
            }),
            dataIndex: 'licensePlate',
        }, {
            title: formatMessage({
                id: 'detailPanel.column.saleDate',
                defaultMessage: '实销日期'
            }),
            dataIndex: 'saleDate',
            render: text => formatDateTime(text, DATE_FORMAT)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.salePrice',
                defaultMessage: '实销价格'
            }),
            dataIndex: 'salePrice',
            render: text => formatAmount(text)
        }, {
            title: formatMessage({
                id: 'detailPanel.column.currency',
                defaultMessage: '币种'
            }),
            dataIndex: 'currency',
            render: text => conventEnumValueToString(currency, text)
        }];
        return (
            <div className="detail">
                <Card loading={this.state.loading}>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.title',
                            defaultMessage: '基础信息'
                        })
                    }</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.code',
                            defaultMessage: '客户编号',
                        })}>{this.props.detail.code}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.name',
                            defaultMessage: '客户姓名',
                        })}>{this.props.detail.name}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.email',
                            defaultMessage: 'Email',
                        })}>{this.props.detail.email}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.cellPhoneNumber',
                            defaultMessage: '客户电话',
                        })}>{this.props.detail.cellPhoneNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.idType',
                            defaultMessage: '证件类型',
                        })}>{conventEnumValueToString(idType, this.props.detail.idType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.idNumber',
                            defaultMessage: '证件号码',
                        })}>{this.props.detail.idNumber}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.gender',
                            defaultMessage: '性别',
                        })}>{conventEnumValueToString(gender, this.props.detail.gender)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.birthday',
                            defaultMessage: '生日',
                        })}>{formatDateTime(this.props.detail.birthday, DATE_FORMAT)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.address',
                            defaultMessage: '地址',
                        })}>{this.props.detail.address}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.filingTime',
                            defaultMessage: '建档时间',
                        })}>{formatDateTime(this.props.detail.filingTime, DATETIME_FORMAT)}</Description>
                    </DescriptionList>
                    <Divider/>
                    <DescriptionList size="large" title={<Row><Col span={12}>{
                        formatMessage({
                            id: 'detailPanel.card.customerproperty',
                            defaultMessage: '客户属性'
                        })
                    }</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.occupationType',
                            defaultMessage: '职业类型',
                        })}>{conventEnumValueToString(customerOccupationType, this.props.detail.occupationType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.industryType',
                            defaultMessage: '行业类型',
                        })}>{conventEnumValueToString(industryType, this.props.detail.industryType)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.diploma',
                            defaultMessage: '学历',
                        })}>{conventEnumValueToString(education, this.props.detail.diploma)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.householdIncome',
                            defaultMessage: '家庭年收入',
                        })}>{conventEnumValueToString(householdIncome, this.props.detail.householdIncome)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.type',
                            defaultMessage: '客户类型',
                        })}>{conventEnumValueToString(customerType, this.props.detail.type)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.hobby',
                            defaultMessage: '客户爱好',
                        })}>{conventEnumValueToString(hobby, this.props.detail.hobby)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.favoriteVehicleFactor',
                            defaultMessage: '购车重视因素',
                        })}>{conventEnumValueToString(favoriteVehicleFactor, this.props.detail.favoriteVehicleFactor)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.ownedVehicleBrand',
                            defaultMessage: '曾购车品牌',
                        })}>{conventEnumValueToString(ownedVehicleBrand, this.props.detail.ownedVehicleBrand)}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isTestDrive',
                            defaultMessage: '是否试乘试驾',
                        })}>{this.props.detail.isTestDrive ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                        <Description term={formatMessage({
                            id: 'detailPanel.data.isLoans',
                            defaultMessage: '是否贷款',
                        })}>{this.props.detail.isLoans ? formatMessage({
                                id: 'detailPage.term.yes',
                                defaultMessage: '是'
                            }) : formatMessage({
                                id: 'detailPage.term.no',
                                defaultMessage: '否'
                            })}</Description>
                    </DescriptionList>
                </Card>
                <Card>
                    <DescriptionList title={formatMessage({
                        id: 'detailPanel.table.details',
                        defaultMessage: '实销信息清单'
                    })}>
                        <Table
                            className="white-space-nowrap"
                            columns={columns}
                            rowKey="id"
                            pagination={false}
                            dataSource={details}
                            {...TABLE} />
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.state.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'detailPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

DetailPanel.propTypes = {
    detail: PropTypes.object.isRequired,
    id: PropTypes.string.isRequired,
    init: PropTypes.func.isRequired,
    loading: PropTypes.bool.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {getDetail, tableSearch} from './actions';
import {injectIntl} from './intl';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getDetailData = selectorFactory(['page', 'domainData', 'detail', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    detail: getDetailData(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    init: () => dispatch(getDetail(props.id)),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(DetailPanel));
